package com.qf.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsBrandMapper;
import com.qf.pojo.DtsBrand;
import com.qf.vo.CatVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

@Service
public class BrandServiceImpl implements BrandService {
    @Autowired
    private DtsBrandMapper brandMapper;

    @Override
    public IPage<DtsBrand> queryByPage(Integer id, String name,
                                       Integer page, Integer limit,
                                       String sort, String order) {
        IPage<DtsBrand> iPage = new Page<>(page, limit);
        QueryWrapper<DtsBrand> queryWrapper = new QueryWrapper<>();

        if (!StringUtils.isEmpty(id)) {
            queryWrapper.eq("id", id);
        }
        if (!StringUtils.isEmpty(name)) {
            queryWrapper.like("name", name);
        }
        //排序
        if ("asc".equalsIgnoreCase(order)) {
            queryWrapper.orderByAsc(sort);
        } else {
            queryWrapper.orderByDesc(sort);
        }
        IPage<DtsBrand> brandIPage = brandMapper.selectPage(iPage, queryWrapper);

        return brandIPage;
    }

    @Override
    public List<CatVo> list() {
        return brandMapper.list();
    }

    @Override
    public DtsBrand insertOne(DtsBrand brand) {
        brand.setAddTime(new Date());
        brand.setUpdateTime(new Date());
        brand.setDeleted(false);
        brandMapper.insert(brand);
        return brand;
    }

    @Override
    public int updateOne(DtsBrand brand) {
        brand.setUpdateTime(new Date());
        int updateById = brandMapper.updateById(brand);
        return updateById;
    }

    @Override
    public void deleteOne(DtsBrand brand) {
        brandMapper.deleteById(brand);
    }
}
